<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 200px;
            height: 30px;
            border: 1px solid black;
        }
    </style>
</head>
<body>
    <button>点击</button>
    <div class="box"></div>
    <script>
        var btn = document.querySelector('button')
        var div = document.querySelector('.box')
        div.innerText = date()

        btn.onclick = function() {
            div.innerText = date()
        }



        function date() {
        var myDate = new Date()
        var y = myDate.getFullYear()
        var m = zeroFill(myDate.getMonth() + 1)
        var d = zeroFill(myDate.getDate())
        var h = zeroFill(myDate.getHours())
        var M = zeroFill(myDate.getMinutes())
        var s = zeroFill(myDate.getSeconds())
        var str = `${y}-${m}-${d}  ${h}:${M}:${s}`
        return str
      }
     

      function zeroFill(n) {
        return n < 10 ? '0' + n : n
      }
    </script>
</body>
</html>